import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:kq_flutter_core_widget/config/kq_core_global.dart';

import '../../resources/l10n/kq_pad_intl.dart';
import 'kq_confirm_dialog.dart';
import 'kq_custom_dialog.dart';
import 'kq_msg_dialog.dart';
import 'kq_opt_dialog.dart';

/// 通用对话框，简化调用过程
class KqPadDialog {
  ///消息对话框
  ///
  /// 顶部标题
  /// 中间内容
  /// 底部1个按钮-我知道了
  static showMsgDialog(
      {String? title,
      required String msg,
      String? btnText,
      bool barrierDismissible = true,
      GestureTapCallback? onBtnTap,
      Function()? onDismiss}) {
    showDialog(
        context: KqCoreGlobal.globalContext,
        barrierDismissible: barrierDismissible,
        barrierColor: Colors.black.withOpacity(0.2),
        builder: (context) {
          return KqPadMsgDialog(
            titleString: title,
            msg: msg,
            btnText: btnText ?? KqPadIntl.currentResource.known,
            onBtnTap: () {
              Navigator.pop(context);
              onBtnTap?.call();
            },
          );
        }).then((value) => onDismiss?.call());
  }

  /// 操作对话框，
  ///
  /// 顶部标题和关闭按钮
  /// 中间自定义内容
  /// 底部1个按钮-确定
  static showOptDialog(
      {String? title,
      required Widget child,
      String? btnText,
      double? width,
      double? height,
      bool barrierDismissible = true,
      GestureTapCallback? onBtnTap,
      Function()? onDismiss}) {
    showDialog(
        context: KqCoreGlobal.globalContext,
        barrierDismissible: barrierDismissible,
        barrierColor: Colors.black.withOpacity(0.2),
        builder: (context) {
          return KqPadOptDialog(
            titleString: title,
            btnText: btnText,
            width: width,
            height: height,
            child: child,
            onBtnTap: () {
              Navigator.pop(context);
              onBtnTap?.call();
            },
          );
        }).then((value) => onDismiss?.call());
  }

  /// 确认对话框，
  ///
  /// 顶部标题
  /// 中间内容
  /// 底部两个按钮-取消和确定
  static showConfirmDialog(
      {String? title,
      required String msg,
      String? leftBtnText,
      String? rightBtnText,
      bool barrierDismissible = true,
      GestureTapCallback? onCancel,
      GestureTapCallback? onConfirm,
      Function()? onDismiss}) {
    showDialog(
        context: KqCoreGlobal.globalContext,
        barrierDismissible: barrierDismissible,
        barrierColor: Colors.black.withOpacity(0.2),
        builder: (context) {
          return KqPadConfirmDialog(
            titleString: title,
            msg: msg,
            leftBtnText: leftBtnText,
            rightBtnText: rightBtnText,
            onLeftBtnTap: () {
              Navigator.pop(context);
              onCancel?.call();
            },
            onRightBtnTap: () {
              Navigator.pop(context);
              onConfirm?.call();
            },
          );
        }).then((value) => onDismiss?.call());
  }

  /// 显示loading
  static void showLoading({String? msg, bool showMask = false}) {
    EasyLoading.show(
        status: msg ?? KqPadIntl.currentResource.loading,
        maskType:
            showMask ? EasyLoadingMaskType.black : EasyLoadingMaskType.clear);
  }

  /// 关闭loading
  static void closeLoading() {
    EasyLoading.dismiss();
  }

  // 自定义弹窗
  static showCustomDialog({
    String? title,
    required Widget customWidget,
    Widget? Function(BuildContext context, Function()? onCancel)?
        customBtnWidget,
    String? leftBtnText,
    String? rightBtnText,
    Color? leftBtnColor,
    Color? rightBtnColor,
    bool barrierDismissible = true,
    Function()? onLeftTap,
    Function()? onRightTap,
    bool isShowLeft = false,
    bool isShowRight = false,
    double? customTopPadding,
    double? customBottomPadding,
    Function()? onDismiss,
  }) {
    showDialog(
        context: KqCoreGlobal.globalContext,
        barrierDismissible: barrierDismissible,
        builder: (context) {
          return KqCustomDialog(
            titleString: title,
            customWidget: customWidget,
            customBtnWidget: customBtnWidget?.call(context, onLeftTap),
            leftBtnText: leftBtnText,
            //左侧灰色
            rightBtnText: rightBtnText,
            //右侧蓝色
            leftBtnColor: leftBtnColor,
            rightBtnColor: rightBtnColor,
            isShowLeft: isShowLeft,
            isShowRight: isShowRight,
            customTopPadding: customTopPadding,
            customBottomPadding: customBottomPadding,
            onLeftBtnTap: () {
              Navigator.pop(context);
              onLeftTap?.call();
            },
            onRightBtnTap: () {
              Navigator.pop(context);
              onRightTap?.call();
            },
          );
        }).then((value) => onDismiss?.call());
  }
}
